package marsdonne.demo4springData.controllers;

import marsdonne.demo4springData.domains.jpa.SqlTask;
import marsdonne.demo4springData.domains.mongo.SqlTask2;
import marsdonne.demo4springData.repositories.mongo.SqlTask2Repository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import javax.transaction.Transactional;

@Transactional
@RestController
@RequestMapping(path = "/sqlTask")
public class SqlTaskController {

    @Autowired
    private marsdonne.demo4springData.repositories.jpa.SqlTaskRepository sqlTaskJpaRepository;

    @Autowired
    private SqlTask2Repository sqlTaskMongoRepository;

    @RequestMapping(produces = "application/vnd.demo-v1+json", path = "/save", method = {RequestMethod.POST, RequestMethod.PUT})
    public String save() {
        SqlTask sqlTask = new SqlTask("select 1", "2017", "1,2,3", false, SqlTask.DbType.PgSQL);

        return "SqlTask(Jpa): #" + sqlTaskJpaRepository.save(sqlTask).getId();
    }

    @RequestMapping(produces = "application/vnd.demo-v2+json", path = "/save", method = {RequestMethod.POST, RequestMethod.PUT})
    public String save2() {
        SqlTask2 sqlTask = new SqlTask2("select 1", "2017", "1,2,3", false, SqlTask.DbType.PgSQL);

        return "SqlTask(Mongo): #" + sqlTaskMongoRepository.save(sqlTask).getId();
    }
}
